﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using DevExpress.Web.ASPxTreeList;

public partial class ControlAllSalesForceTree : System.Web.UI.UserControl
{
   
    ClassExecute.ClassExecute ex;
   
    string error;
    string str = BCDataCenter.Configuration.CurrentConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (IsPostBack)
        {
            try
            {
                if (ViewState["db"] != null)
                {
                    DataTable db = (DataTable)ViewState["db"];
                    ASPxTreeList1.DataSource = db;
                    ASPxTreeList1.DataBind();
                }
            }
            catch
            {
                return;
            }
        }
        else
        {
            getListTimeReport();  
           
        }
    }
    void getListTimeReport()
    {
        ex=new ClassExecute.ClassExecute();
        DataTable month=ex.Get_Data_DataTable(str,ref error,"k_getMonthList",CommandType.StoredProcedure);
        DropDownListDateReport.DataSource = month;
        DropDownListDateReport.DataTextField = "Names";
        DropDownListDateReport.DataValueField = "Values";
        DropDownListDateReport.DataBind();
       

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            ASPxTreeList1.ExpandAll();
            ASPxTreeListExporter1.WriteXlsToResponse(DateTime.Now.Date.ToString() + "_AllSalesForceTree.xls");
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ASPxTreeList1_HtmlRowPrepared(object sender, DevExpress.Web.ASPxTreeList.TreeListHtmlRowEventArgs e)
    {
        try
        {
            if (e.RowKind == TreeListRowKind.Data)
            {

                if (e.GetValue("CấpBậc").ToString() == "BUM")
                {
                    e.Row.BackColor = Color.Yellow;
                }
                else if (e.GetValue("CấpBậc").ToString() == "SUM")
                {
                    e.Row.BackColor = Color.BlueViolet;
                }
                else if (e.GetValue("CấpBậc").ToString() == "ESM")
                {
                    e.Row.BackColor = Color.Blue;
                }
                else if (e.GetValue("CấpBậc").ToString() == "EDM")
                {
                    e.Row.BackColor = Color.Aqua;
                }
                else if (e.GetValue("CấpBậc").ToString() == "SDM")
                {
                    e.Row.BackColor = Color.CadetBlue;
                }
                else if (e.GetValue("CấpBậc").ToString() == "EDD")
                {
                    e.Row.BackColor = Color.Coral;
                }
                if (e.GetValue("isPreBum").ToString() == "True")
                {
                    e.Row.BackColor = Color.Red;
                }
            }
        }
        catch
        {
            return;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        string vUserName = "";
        if (Session["username"].ToString() == "DefaultUser")
        {
            vUserName = Session["Salesp_key_Logon"].ToString();
        }
        else
        {
            vUserName = Session["username"].ToString();
        }

        SqlConnection conn = null;
        int i = 0;
        try
        {

            // create and open a connection object
            conn = new SqlConnection(str);
            conn.Open();

            // 1.  create a command object identifying
            //     the stored procedure
            SqlCommand cmd = new SqlCommand("[ls_GenerateAllSalesForce_CS2011]", conn);

            // 2. set the command object so it knows
            //    to execute a stored procedure
            cmd.CommandType = CommandType.StoredProcedure;

            // 3. add parameter to command, which
            //    will be passed to the stored procedure
            cmd.Parameters.Add(new SqlParameter("@MmYyyy", DropDownListDateReport.SelectedValue.ToString()));
            cmd.Parameters.Add(new SqlParameter("@UserName", vUserName));
            cmd.CommandTimeout = 300;
            cmd.ExecuteNonQuery();
            i = 1;
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
        finally
        {
            conn.Close();
        }
        if (i == 1)
        {
            try
            {
                ex = new ClassExecute.ClassExecute();
                if (ViewState["db"] != null)
                {
                    ViewState["db"] = null;
                }
                DataTable db = ex.Get_Data_DataTable(str, ref error, "[ls_getAllSalesForce4Tree_CS2012]", CommandType.StoredProcedure,
                     new SqlParameter("@UserName", vUserName),
                     new SqlParameter("@YyyyMm", DropDownListDateReport.SelectedValue.ToString().Substring(2) + DropDownListDateReport.SelectedValue.ToString().Substring(0, 2)),
                     new SqlParameter("@RootBA", txtMaTVV.Text.Trim()),
                     new SqlParameter("@mBARange", Int32.Parse(rbl1.SelectedValue.ToString())));
               
                ViewState["db"] = db;
                if (db.Rows.Count > 0)
                {
                    ASPxTreeList1.DataSource = db;
                    ASPxTreeList1.DataBind();

                    for (int j = 0; j < ASPxTreeList1.Columns.Count; j++)
                    {
                        TreeListDataColumn col = ASPxTreeList1.Columns[j] as TreeListDataColumn;
                        bool check = IsNumeric(col.FieldName.Substring(0, 1));

                        if (check)
                        {
                            col.DisplayFormat = "{0:#,###}";

                        }

                    }
                }
               

            }
            catch (Exception exe)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(exe.Message + error, method);
            }
        }
        
    }
    protected void btExpand_Click(object sender, EventArgs e)
    {
        ASPxTreeList1.ExpandAll();
    }
    protected void btColap_Click(object sender, EventArgs e)
    {
        ASPxTreeList1.CollapseAll();
    }
    void BindTree()
    {
        ex = new ClassExecute.ClassExecute();
        DataTable db = ex.Get_Data_DataTable(str, ref error, "[ls_getAllSalesForce4Tree_CS2012]", CommandType.StoredProcedure,
            new SqlParameter("@UserName", "Admin"),
            new SqlParameter("@YyyyMm", "201111"),
            new SqlParameter("@RootBA", "BC"),
            new SqlParameter("@mBARange", 1));
       

        ASPxTreeList1.DataSource = db;
        ASPxTreeList1.DataBind();



        for (int i = 0; i < ASPxTreeList1.Columns.Count; i++)
        {
            TreeListDataColumn col = ASPxTreeList1.Columns[i] as TreeListDataColumn;
            bool check = IsNumeric(col.FieldName.Substring(0, 1));

            if (check)
            {
                col.DisplayFormat = "{0:#,###}";

            }

        }

    }
    public bool IsNumeric(string anyString)
    {
        if (anyString == null)
        {
            anyString = "";
        }
        if (anyString.Length > 0)
        {
            double dummyOut = new double();
            System.Globalization.CultureInfo cultureInfo =
                new System.Globalization.CultureInfo("en-US", true);

            return Double.TryParse(anyString, System.Globalization.NumberStyles.Any,
                cultureInfo.NumberFormat, out dummyOut);
        }
        else
        {
            return false;
        }
    }
}
